package com.fr.data.core.db.dml;

import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.list.IntList;
import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Insert extends InsertOrUpdate
{
  private Map columnNameExpressionMap;

  public Insert(Table paramTable)
  {
    this(paramTable, DialectFactory.getDefaultDialect());
  }

  public Insert(Table paramTable, Dialect paramDialect)
  {
    super(paramTable, paramDialect);
    this.columnNameExpressionMap = new HashMap();
  }

  public void putNameExpression(String paramString1, String paramString2)
  {
    this.columnNameExpressionMap.put(paramString1, paramString2);
  }

  public boolean isSelfincreaseColumn(String paramString)
  {
    return true;
  }

  public boolean isUpdate()
  {
    return false;
  }

  public PreparedStatement createPreparedStatement(Connection paramConnection)
    throws SQLException
  {
    if ((paramConnection == null) || (getTable() == null))
      return null;
    if (this.dialect == null)
      this.dialect = DialectFactory.getDefaultDialect();
    IntList localIntList = new IntList();
    ArrayList localArrayList1 = new ArrayList();
    ArrayList localArrayList2 = new ArrayList();
    int i = 0;
    int j = getColumnCount();
    while (i < j)
    {
      if (!(DBUtils.isNULL(getColumnValue(i))))
      {
        localArrayList1.add(this.dialect.column2SQL(getColumnName(i)));
        localArrayList2.add("?");
        localIntList.add(i);
      }
      ++i;
    }
    Iterator localIterator = this.columnNameExpressionMap.entrySet().iterator();
    while (localIterator.hasNext())
    {
      localObject = (Map.Entry)localIterator.next();
      localArrayList1.add(this.dialect.column2SQL((String)((Map.Entry)localObject).getKey()));
      localArrayList2.add(((Map.Entry)localObject).getValue());
    }
    if (localArrayList2.size() == 0)
      return null;
    Object localObject = "INSERT INTO " + getTable().toStatementSQLString(this.dialect) + " (" + BaseCoreUtils.join(localArrayList1, ",") + ")" + " VALUES (" + BaseCoreUtils.join(localArrayList2, ",") + ")";
    FRContext.getLogger().log(Level.INFO, ((String)localObject).toString());
    PreparedStatement localPreparedStatement = paramConnection.prepareStatement(((String)localObject).toString());
    for (int k = 0; k < localIntList.size(); ++k)
    {
      int l = localIntList.get(k);
      DBUtils.applyColumnTypeValue(this.dialect, paramConnection, getTable(), getColumnName(l), localPreparedStatement, k + 1, getColumnType(l), getColumnValue(l));
    }
    return ((PreparedStatement)localPreparedStatement);
  }

  public int execute(Connection paramConnection)
    throws SQLException
  {
    PreparedStatement localPreparedStatement = createPreparedStatement(paramConnection);
    try
    {
      if (localPreparedStatement != null)
      {
        int i = localPreparedStatement.executeUpdate();
        return i;
      }
    }
    finally
    {
      DBUtils.closeStatement(localPreparedStatement);
    }
    return 0;
  }
}